//
//  CloseCourseView.m
//  ZYZB
//
//  Created by ENERGY on 2018/7/4.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "CloseCourseView.h"

@implementation CloseCourseView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIButton * backgroundBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height)];
        [backgroundBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        backgroundBtn.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:0.75];
        [self addSubview:backgroundBtn];
        
        [self setUpViews];
    }return self;
}
#pragma mark - init
- (void)setUpViews{
    //背景
    UIView * whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    whiteView.layer.cornerRadius = 6;
    whiteView.layer.masksToBounds = YES;
    [self addSubview:whiteView];
    if (Is_Iphone_X) {
        whiteView.frame = CGRectMake(18/WIDTH_6_SCALE, 180/WIDTH_6_SCALE + 24, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 200/WIDTH_6_SCALE);
    }else{
        whiteView.frame = CGRectMake(18/WIDTH_6_SCALE, 180/WIDTH_6_SCALE, SCREEN_WIDTH - 36/WIDTH_6_SCALE, 200/WIDTH_6_SCALE);
    }
    //圆圈图案
    UIImageView * circleView = [[UIImageView alloc] initWithFrame:CGRectMake(44/WIDTH_6_SCALE, 38/WIDTH_6_SCALE, 18/WIDTH_6_SCALE, 18/WIDTH_6_SCALE)];
//    circleView.backgroundColor = [UIColor orangeColor];
    circleView.image = [UIImage imageNamed:@"warn"];
    [whiteView addSubview:circleView];
    //关闭按钮
    int closeBtnW = 16/WIDTH_6_SCALE;
    UIButton * closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(whiteView.width - closeBtnW - 16/WIDTH_6_SCALE, 16/WIDTH_6_SCALE, closeBtnW, closeBtnW)];
    closeBtn.layer.cornerRadius = closeBtnW/2;
    closeBtn.layer.masksToBounds = YES;
//    [closeBtn setBackgroundColor:[UIColor orangeColor]];
    [closeBtn setImage:[UIImage imageNamed:@"lend_cross"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(noCloseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [whiteView addSubview:closeBtn];
    //标题
    UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(circleView.right + 5/WIDTH_6_SCALE, 0, 120/WIDTH_6_SCALE, 14/WIDTH_6_SCALE)];
    titleLab.centerY = circleView.centerY;
    titleLab.text = @"福乐猪提醒您";
    titleLab.font = DEF_FontSize_14;
    titleLab.textColor = [UIColor blackColor];
    [whiteView addSubview:titleLab];
    //提醒内容
    UILabel * contentLab = [[UILabel alloc] initWithFrame:CGRectMake(circleView.left, circleView.bottom + 22/WIDTH_6_SCALE, whiteView.width - 88/WIDTH_6_SCALE, 34/WIDTH_6_SCALE)];
    contentLab.text = @"关闭后，不再显示。将无法查看“出借教程”。是否确定关闭呢？";
    contentLab.textColor = getColor(@"bcbcbc");
    contentLab.lineBreakMode = 0;
    contentLab.numberOfLines = 0;
    contentLab.font = DEF_FontSize_12;
    [whiteView addSubview:contentLab];
    //分割线
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, contentLab.bottom + 26/WIDTH_6_SCALE, SCREEN_WIDTH, 1/WIDTH_6_SCALE)];
    lineView.backgroundColor = getColor(borderColor);
    [whiteView addSubview:lineView];
    //确认关闭
    UIButton * sureCloseBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, lineView.bottom, whiteView.width/2, whiteView.height - lineView.top - 1/WIDTH_6_SCALE)];
    [sureCloseBtn setTitle:@"确认关闭" forState:UIControlStateNormal];
    sureCloseBtn.titleLabel.font = DEF_FontSize_12;
    [sureCloseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [whiteView addSubview:sureCloseBtn];
    [sureCloseBtn addTarget:self action:@selector(sureCloseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    //不想关闭
    UIButton * noCloseBtn = [[UIButton alloc] initWithFrame:CGRectMake(sureCloseBtn.right, sureCloseBtn.top, sureCloseBtn.width, sureCloseBtn.height)];
    [noCloseBtn setTitle:@"不想关闭" forState:UIControlStateNormal];
    noCloseBtn.titleLabel.font = DEF_FontSize_12;
    [noCloseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [whiteView addSubview:noCloseBtn];
    [noCloseBtn addTarget:self action:@selector(noCloseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - action
- (void)showView{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)cancelBtnClick{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)sureCloseBtnClick:(UIButton *)sender{
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"ifShutCourse"];
    [userDefaults synchronize];
    if (self.sureBtnClickBlock) {
        self.sureBtnClickBlock();
    }
    [self cancelBtnClick];
}
- (void)noCloseBtnClick:(UIButton *)sender{
    [self cancelBtnClick];
}
@end
